import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import _XEUtils_ from 'xe-utils'
import { useRouteStore } from '@/stores/index.js'

const storeSetup = () => {
    const router = useRouter()
    const tabList = ref([]) // 保存页签tab的数组
    const cacheList = ref([]) // keep-alive缓存的数组，元素是组件名

    // 添加一个页签，如果当前路由已经打开，则不再重复添加
    const addTabItem = (item) => {
        const index = tabList.value.findIndex((i) => i.path === item.path)
        if (index >= 0) {
            tabList.value[index].fullPath !== item.fullPath && (tabList.value[index] = item)
        } else {
            if (item.meta?.showInTabs ?? true) {
                tabList.value.push(item)
            }
        }
    }

    // 删除一个页签
    const deleteTabItem = (path) => {
        const index = tabList.value.findIndex((item) => item.path === path && !item.meta?.affix)
        if (index < 0) return
        const isActive = router.currentRoute.value.path === tabList.value[index].path
        tabList.value.splice(index, 1)
        if (isActive) {
            const lastObj = tabList.value[tabList.value.length - 1]
            router.push(lastObj.fullPath || lastObj.path)
        }
    }

    // 清空页签
    const clearTabList = () => {
        const routeStore = useRouteStore()
        const arr = []
        _XEUtils_.eachTree(routeStore.routes, (item) => {
            if (item.meta?.affix ?? false) {
                arr.push(item)
            }
        })
        tabList.value = arr
    }

    // 添加缓存页
    const addCacheItem = (item) => {
        if (!item.name) return
        if (cacheList.value.includes(item.name)) return
        if (item.meta?.keepAlive) {
            cacheList.value.push(item.name)
        }
    }

    // 删除一个缓存页
    const deleteCacheItem = (name) => {
        const index = cacheList.value.findIndex((i) => i === name)
        if (index >= 0) {
            cacheList.value.splice(index, 1)
        }
    }

    // 清空缓存页
    const clearCacheList = () => {
        cacheList.value = []
    }

    // 关闭当前
    const closeCurrent = (path) => {
        const item = tabList.value.find((i) => i.path === path)
        item?.name && deleteCacheItem(item.name)
        deleteTabItem(path)
    }

    // 关闭其他
    const closeOther = (path) => {
        const arr = tabList.value.filter((i) => i.path !== path)
        arr.forEach((item) => {
            deleteTabItem(item.path)
            item?.name && deleteCacheItem(item.name)
        })
    }

    // 关闭右侧
    const closeRight = (path) => {
        const index = tabList.value.findIndex((i) => i.path === path)
        if (index < 0) return
        const arr = tabList.value.filter((i, n) => n > index)
        arr.forEach((item) => {
            deleteTabItem(item.path)
            item?.name && deleteCacheItem(item.name)
        })
    }

    // 关闭全部
    const closeAll = () => {
        clearTabList()
        clearCacheList()
        router.push({ path: '/' })
    }

    // 重置
    const reset = () => {
        clearTabList()
        clearCacheList()
    }

    // 初始化
    const init = () => {
        if (tabList.value.some((i) => !i?.meta.affix)) return
        reset()
    }

    return {
        tabList,
        cacheList,
        addTabItem,
        deleteTabItem,
        clearTabList,
        addCacheItem,
        deleteCacheItem,
        clearCacheList,
        closeCurrent,
        closeOther,
        closeRight,
        closeAll,
        reset,
        init
    }
}

export const useTabsStore = defineStore('tabs', storeSetup, { persist: { storage: sessionStorage } })
